/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { btoa, atob } from "./base64.js"
import {str,ASCIIstr, str100K, ASCIIstr100K} from './string.js'
//编码  btoa   创建一个Base-64 编码的 ASCII 字符串 

function base64_test_btoa_001() {
    let message
    let startTime = Date.now();
    for (let index = 0; index < 100; index++) {
        message = btoa(str)
    }
    let endTime = Date.now();
    print(`base64_test_btoa: ${endTime - startTime} ms`)
    if (message !== ASCIIstr) {
        throw new Error('base64_test_btoa err')
    }
}

function base64_test_btoa_100K_001() {
    let message
    let startTime = Date.now();
    for (let index = 0; index < 1; index++) {
        message = btoa(str100K)
    }
    let endTime = Date.now();
    print(`base64_test_btoa_100KB: ${endTime - startTime} ms`)
    if (message !== ASCIIstr100K) {
        throw new Error('base64_test_btoa err')
    }
}

function base64_test_atob_002(){
    let message
    let startTime = Date.now();
    for (let index = 0; index < 100; index++) {
        message = atob(ASCIIstr)
    }
    let endTime = Date.now();
    print(`base64_test_atob: ${endTime - startTime} ms`)
    if (str !== message) {
        throw new Error('base64_test_atob err')
    }
}

function base64_test_atob_100K_002(){
    let message
    let startTime = Date.now();
    for (let index = 0; index < 1; index++) {
        message = atob(ASCIIstr100K)
    }
    let endTime = Date.now();
    print(`base64_test_atob_100KB: ${endTime - startTime} ms`)
    if (str100K !== message) {
        throw new Error('base64_test_atob err')
    }
}


let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    base64_test_btoa_001()
    base64_test_atob_002()
    base64_test_btoa_100K_001()
    base64_test_atob_100K_002()
}

ArkTools.waitAllJitCompileFinish();

base64_test_btoa_001()
base64_test_atob_002()
base64_test_btoa_100K_001()
base64_test_atob_100K_002()
